export const isEmpty = (str) => {
  return (!str || 0 === str.length);
};

export const isBlank = (str) => {
  return (!str || /^\s*$/.test(str));
};

export const processResp = (resp) => {

  if (resp.bodyText) {

    if (resp.bodyText.promise) {
      return resp;
    } else {
      let {responseHeader} = resp.data;

      if (responseHeader.status === 0) {
        throw {
          errCode : responseHeader.errorCode,
          errMsg: responseHeader.errorMessage
        };
      } else {
        return resp.data.payload;
      }
    }

  } else {
    return resp;
  }

};

export const getUsername = (token) => {

  let payload = null;

  if (token) {
    payload = token.split('.')[1];
  } else if (window.sessionStorage.token) {
    payload = window.sessionStorage.token.split('.')[1];
  }

  return payload ? JSON.parse(window.atob(payload)).account : null;

};

export const getHeaders = () => {

  if (window.sessionStorage.token) {
    return {
      headers: {
        Authorization: "Bearer " + window.sessionStorage.token
      }
    };
  } else {
    return null;
  }

};

export default {
  isEmpty,
  isBlank,
  processResp,
  getUsername,
  getHeaders
};
